//
//  ZTodoMissionVC.m
//  WeClubs
//
//  Created by Zed Link on 8/4/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZTodoMissionVC.h"
#import "ZTodoMissionCell.h"
#import "ZTodoMissionDetailVC.h"
#import "ZTodoFlowLayout.h"

static NSString *MissionCellReuseID = @"ZMissionCellReuseID";

@interface ZTodoMissionVC () <UICollectionViewDelegate, UICollectionViewDataSource>

/** 待办任务列表 */
@property (weak, nonatomic) UICollectionView *missionView;

/** 待办任务数据源 */
@property (strong, nonatomic) NSArray <ZTodoMissionModel *> *missionModels;

@end

@implementation ZTodoMissionVC


- (void)zLoadSubview
{
    [super zLoadSubview];
    
    // ========== 待办任务列表 ==========
    ZTodoFlowLayout *layout = [[ZTodoFlowLayout alloc] init];
    
    UICollectionView *missionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    missionView.delegate = self;
    missionView.dataSource = self;
    
    [missionView setBackgroundColor:GENERAL_VIEW_BG_COLOR];
    [missionView registerNib:[UINib nibWithNibName:@"ZTodoMissionCell" bundle:nil] forCellWithReuseIdentifier:MissionCellReuseID];
    missionView.contentInset = UIEdgeInsetsMake(DYNAMIC_TOP_SPACE, 0, DYNAMIC_TOP_SPACE, 0);
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.missionView = missionView;
    [self.view addSubview:missionView];
    [self.missionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSDictionary *dataPara = @{
                               @"user_id" : @([ZUserInfo sharedInstance].userId),
                               @"token" : [ZUserInfo sharedInstance].token,
                               @"club_id" : @(self.clubModel.clubId),
                               @"dynamic_type" : @"mission",
                               @"page_no" : @(0),
                               @"size" : @(8),
                               };
    [HTTPRequest POST:kGetMyDynamicListURL parameter:dataPara success:^(id responseObject) {
        // Todo: 加载下一页数据
        NSArray *missions = responseObject[@"data"][@"mission"];
        
        NSMutableArray *missionModels = [NSMutableArray arrayWithCapacity:missions.count];
        for (NSDictionary *dict in missions) {
            ZTodoMissionModel *model = [[ZTodoMissionModel alloc] initWithDictionary:dict];
            [missionModels addObject:model];
        }
        self.missionModels = missionModels;
    } failure:^(NSError *error) {
        
    }];
    
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.missionModels.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZTodoMissionModel *model = self.missionModels[indexPath.row];
    
    ZTodoMissionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MissionCellReuseID forIndexPath:indexPath];
    cell.type = ZMissionTypeTodo;
    cell.model = model;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZTodoMissionModel *model = self.missionModels[indexPath.row];
    
    ZTodoMissionDetailVC *detailVC = [ZTodoMissionDetailVC zNew];
    detailVC.missionModel = model;
    [self.navigationController pushViewController:detailVC animated:YES];
}


#pragma mark - Getter & Setter

- (void)setMissionModels:(NSArray<ZTodoMissionModel *> *)missionModels
{
    _missionModels = missionModels;
    [self.missionView reloadData];
}

@end
